package com.sun.officeoa.controller;

import com.sun.officeoa.bean.Employee;
import com.sun.officeoa.service.EmployeeService;
import com.sun.officeoa.util.StringUtil;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/* ━━━━━━如来保佑━━━━━━
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　┻　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━永无BUG━━━━━━
 * 技术大咖-SUN-JM
 * @Date: 2019/3/4 09:24
 * @Description:  TODO
 */
@Controller
@RequestMapping(value = "/office-oa")
public class ApproveController {

    @Resource
    private EmployeeService employeeService;
    
    /**
     * @Author SUN-JM
     * @Description //TODO 查询未审批得人员申请
     * @Date 11:29 2019/3/4
     * @Param [errorMsg]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/approve/queryList")
    public ModelAndView queryList(String errorMsg){
        ModelAndView modelAndView = new ModelAndView();
        List<Employee> list = employeeService.getUnApproveaccount();
        if(!StringUtil.isNullOrEmpty(errorMsg)){
            modelAndView.addObject("error", errorMsg);
        }
        modelAndView.addObject("list", list);
        modelAndView.setViewName("/approveaccount");
        return modelAndView;
    }

    /**
     * @Author SUN-JM
     * @Description //TODO 审批接口
     * @Date 11:30 2019/3/4
     * @Param [request]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/approve/updateStatus")
    public ModelAndView updateStatus(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        String status = request.getParameter("status");
        String empid = request.getParameter("empid");
        if(!StringUtil.isNullOrEmpty(status) && !StringUtil.isNullOrEmpty(empid)){
            int i = employeeService.updateEmpStatusById(Integer.parseInt(empid), Integer.parseInt(status));
            if (i != 1) {
                modelAndView = this.queryList("审批失败");
            }
            modelAndView = this.queryList("审批成功");
        }
        return modelAndView;
    }
    /**
     * @Author SUN-JM
     * @Description //TODO 查询员工
     * @Date 11:31 2019/3/4
     * @Param [request]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping("/approve/serachemp")
    public ModelAndView serachemp(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        String updateStatus = request.getParameter("updateStatus");
        if (!StringUtil.isNullOrEmpty(updateStatus) && "-1".equals(updateStatus)) {
            String empid = request.getParameter("empid");
            employeeService.updateEmpStatusById(Integer.parseInt(empid), Integer.parseInt(updateStatus));
        }
        String employeename = request.getParameter("employeename");
        String username = request.getParameter("username");
        String status = request.getParameter("status");
        String page = request.getParameter("page");
        String count = request.getParameter("count");
        if (status == null || "".equals(status)) {
            status = "1";
        }
        if (page == null || "".equals(page)) {
            page = "1";
        }
        if (count == null || "".equals(count)) {
            count = "10";
        }
        List<Employee> list = employeeService.searchEmp(employeename, username, Integer.parseInt(status), Integer.parseInt(page), Integer.parseInt(count));
        int totalCount = employeeService.getCount(employeename, username, Integer.parseInt(status));
        int totalPage = totalCount / Integer.parseInt(count) + 1;
        modelAndView.addObject("list", list);
        modelAndView.addObject("totalCount", totalCount);
        modelAndView.addObject("totalPage", totalPage);
        modelAndView.addObject("page", page);
        modelAndView.addObject("employeename", employeename);
        modelAndView.addObject("username", username);
        modelAndView.addObject("status", status);
        modelAndView.setViewName("/searchemployees");
        return modelAndView;
    }


}
